package org.teachertools.services;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.teachertools.common.ValidationUtils;
import org.teachertools.data.Note;

@Repository
public class NoteService extends GenericService<Note> {

	@Override
	protected Class<Note> getObjectClass() {
		return Note.class;
	}

	@Override
	protected Query getSearchQuery(Note obj) {
		StringBuffer request = new StringBuffer();
		request.append("from Note entity");
		request.append(" left join fetch entity.eleve left join fetch entity.competence");

		request.append(" where 1 = 1");

		if (ValidationUtils.isNotEmpty(obj.getNote())) {
			request.append(" and upper(entity.note) like upper(:label_1)");
		}
		if (ValidationUtils.isNotEmpty(obj.getDateNote())) {
			request.append(" and entity.dateNote between :label_2");
			if (ValidationUtils.isNotEmpty(obj.getFinDateNote()))
				request.append(" and :label_2_bis");
			else
				request.append(" and current_date()");
		}

		if (ValidationUtils.isNotEmpty(obj.getEleve())
				&& ValidationUtils.isNotEmpty(obj.getEleve().getId())) {
			request.append(" and entity.eleve.id = :label_3");
		}
		if (ValidationUtils.isNotEmpty(obj.getCompetence())) {
			if (ValidationUtils.isNotEmpty(obj.getCompetence().getId())) {
				request.append(" and entity.competence.id = :label_4");
			}
			if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere())) {
				if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere()
						.getId())) {
					request
							.append(" and entity.competence.matiere.id = :label_5");
				}
				if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere()
						.getCategorie())) {
					if (obj.getCompetence().getMatiere().getCategorie().getId() != null)
						request
								.append(" and entity.competence.matiere.categorie.id = :label_6");
				}
			}
		}

		Query query = entityManager.createQuery(request.toString());

		if (ValidationUtils.isNotEmpty(obj.getNote())) {
			query.setParameter("label_1", prepareQueryParam(obj.getNote()));
		}
		if (ValidationUtils.isNotEmpty(obj.getDateNote())) {
			query.setParameter("label_2", obj.getDateNote());
			if (ValidationUtils.isNotEmpty(obj.getFinDateNote())) {
				query.setParameter("label_2_bis", obj.getFinDateNote());
			}
		}
		if (ValidationUtils.isNotEmpty(obj.getEleve())
				&& ValidationUtils.isNotEmpty(obj.getEleve().getId())) {
			query.setParameter("label_3", obj.getEleve().getId());
		}
		if (ValidationUtils.isNotEmpty(obj.getCompetence())) {
			if (ValidationUtils.isNotEmpty(obj.getCompetence().getId())) {
				query.setParameter("label_4", obj.getCompetence().getId());
			}
			if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere())) {
				if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere()
						.getId())) {
					query.setParameter("label_5", obj.getCompetence()
							.getMatiere().getId());
				}
				if (ValidationUtils.isNotEmpty(obj.getCompetence().getMatiere()
						.getCategorie())) {
					if (ValidationUtils.isNotEmpty(obj.getCompetence()
							.getMatiere().getCategorie().getId())) {
						query.setParameter("label_6", obj.getCompetence()
								.getMatiere().getCategorie().getId());
					}
				}
			}
		}
		return query.setMaxResults(getMaxResults());
	}
}
